package leetcode.offer;

import leetcode.base.TreeNode;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * ClassName: Offer34_PathSum
 * Description: leetcode-剑指offer 34 二叉树中和为某一值的路径
 * date: 2021-11-10 17:48
 *
 * @author liyifan
 */
public class Offer34_PathSum {

    List<List<Integer>> res = new LinkedList<>();

    Deque<Integer> path = new LinkedList<>();

    public List<List<Integer>> pathSum(TreeNode root, int target) {
        dfs(root, target);
        return res;
    }

    public void dfs(TreeNode root, int target){
        if (root == null)
            return;

        path.offerLast(root.val);
        target -= root.val;
        if (root.left == null && root.right == null && target == 0)
            res.add(new LinkedList<>(path));

        dfs(root.left, target);
        dfs(root.right, target);
        path.pollLast();
    }
}
